{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "84c4794a",
   "metadata": {},
   "source": [
    "## Nomogram\n",
    "\n",
    "读取`mydir`数据的，尽量保证每个数据都是英文编码。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fb9e7879",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from onekey_algo.custom.components import nomogram\n",
    "from onekey_algo import OnekeyDS as okds\n",
    "\n",
    "mydir = r''\n",
    "mydir = okds.survival\n",
    "df = pd.read_csv(mydir, header=0)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "def05a4c",
   "metadata": {},
   "source": [
    "### 绘制nomogram\n",
    "\n",
    "绘制的接口和参数，COX回归进行生存时间预测。\n",
    "\n",
    "```python\n",
    "def nomogram(df: Union[str, DataFrame], duration: str, result: str, columns: Union[str, List[str]],\n",
    "             survs: Union[int, List[int]], surv_names: Union[str, List[str]] = None,\n",
    "             width: int = 960, height: int = 480) -> Image:\n",
    "    \"\"\"\n",
    "    绘制nomogram图，Nomogram的图存储在当前文件夹下的nomogram.png\n",
    "    Args:\n",
    "        df: 数据路径，或者是读取之后的Dataframe格式。\n",
    "        duration: OS\n",
    "        result: OST\n",
    "        columns: 使用那些列计算nomogram\n",
    "        survs: 生存时间转化成x 年生存率\n",
    "        surv_names: survs对应的列名。\n",
    "        width: nomogram分辨率--宽度，默认960\n",
    "        height: nomogram分辨率--宽度，默认480\n",
    "\n",
    "    Returns: PIL.Image\n",
    "    \"\"\"\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99013dd5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 绘制Nomagram\n",
    "nomogram.nomogram(df, duration='duration', result='result', \n",
    "                  columns=['age', 'gender', 'degree', 'Tstage', 'BMI', 'chemotherapy'],\n",
    "                  survs=[36, 60], \n",
    "                  surv_names=['3-year Survival', '5-year Survival'], \n",
    "                  height=5800)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2d47ca6",
   "metadata": {},
   "source": [
    "## 绘制 Risk-nomogram\n",
    "\n",
    "绘制的接口和参数，使用的是logisitc回归进行风险模型建模\n",
    "\n",
    "```python\n",
    "def risk_nomogram(df: Union[str, DataFrame], result: str, columns: Union[str, List[str]],\n",
    "                  width: int = 960, height: int = 480) -> Image:\n",
    "    \"\"\"\n",
    "    绘制nomogram图，Nomogram的图存储在当前文件夹下的nomogram.png\n",
    "    Args:\n",
    "        df: 数据路径，或者是读取之后的Dataframe格式。\n",
    "        result: OST\n",
    "        columns: 使用那些列计算nomogram\n",
    "        width: nomogram分辨率--宽度，默认960\n",
    "        height: nomogram分辨率--宽度，默认480\n",
    "\n",
    "    Returns: PIL.Image\n",
    "    \"\"\"\n",
    " ```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6edc49b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "nomogram.risk_nomogram(df, result='result', columns=['age', 'gender', 'degree', 'Tstage', 'BMI', 'chemotherapy'], \n",
    "                       height=5800)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "31a55a1f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
